import { message } from 'ant-design-vue';
import useUserStore from '@/store/modules/user';

export function useSendMessage() {
  const userStore = useUserStore();
  const userId = ref<string>('');
  const account = ref<string>('');
  // 获取当前时间
  function getCurrentTimeFormatted(): string {
    const now = new Date();
    const year = now.getFullYear();
    const month = String(now.getMonth() + 1).padStart(2, '0');
    const day = String(now.getDate()).padStart(2, '0');
    const hours = String(now.getHours()).padStart(2, '0');
    const minutes = String(now.getMinutes()).padStart(2, '0');
    const seconds = String(now.getSeconds()).padStart(2, '0');
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  }
  // 获取本机发送数据
  const getSendItem = (accNbr: string, orderContent: string, requestTime: string) => {
    const item = {
      accNbr,
      contentParam: '',
      extOrderId: '',
      orderContent,
      password: '141024',
      requestTime,
      sceneId: '7812',
      sendDate: '',
      lanId: '571',
      systemCode: 'SXDXMP',
      transactionId: 'SXDXMP202205050921580000024809',
      userAcct: 'SXDXMP',
    };
    return item;
  };
  // 获取本机发送存储数据
  const getSendData = (id: number, accNbr: string, orderContent: string, requestTime: string, state: string) => {
    const data = {
      id,
      transactionId: 'SXDXMP202205050921580000024809',
      systemCode: 'SXDXMP',
      password: '141024',
      userAcct: 'SXDXMP',
      requestTime,
      accNbr,
      lanId: '571',
      sendDate: '',
      sceneId: '7812',
      orderContent,
      contentParam: '',
      extorderId: '',
      state,
    };
    return data;
  };
  const getSendData2 = (accNbr: string, orderContent: string, requestTime: string, state: string) => {
    const data = {
      transactionId: 'SXDXMP202205050921580000024809',
      systemCode: 'SXDXMP',
      password: '141024',
      userAcct: 'SXDXMP',
      requestTime,
      accNbr,
      lanId: '571',
      sendDate: '',
      sceneId: '7812',
      orderContent,
      contentParam: '',
      extorderId: '',
      state,
    };
    return data;
  };
  // 本机(添加记录)
  const sendMessage = async (accNbr: string, orderContent: string, requestTime: string): Promise<boolean> => {
    const messageItem = getSendItem(accNbr, orderContent, requestTime);
    let state = '';
    let flag: boolean = true;
    await getSendMessage(messageItem).then(async () => {
      message.success('发送成功！');
      // 存储数据
      state = '发送成功';
    }).catch((error) => {
      message.error('发送失败！');
      // 存储数据
      state = `发送失败:${error}`;
      flag = false;
    });
    const messagedata = getSendData2(accNbr, orderContent, requestTime, state);
    await insertUccpMessageParams(messagedata);
    return flag;
  };
  // 本机(修改记录)
  const sendMessageHistory = async (accNbr: string, orderContent: string, requestTime: string, id: number) => {
    const messageItem = getSendItem(accNbr, orderContent, requestTime);
    let state = '';
    await getSendMessage(messageItem).then(async () => {
      message.success('发送成功！');
      state = '发送成功';
    }).catch((error) => {
      state = `发送失败${error}`;
      message.error('发送失败！');
    });
    const messagedata = getSendData(id, accNbr, orderContent, requestTime, state);
    await updateUccpMessageParams(messagedata);
  };
  // 服务器(批量)
  const sendMessageByServer = async (items: any[]): Promise<boolean> => {
    let flag: boolean = true;
    await sendMoreUccpMessage(items).then(async () => {
      message.success('发送成功！');
    }).catch(() => {
      message.success('发送失败！');
      flag = false;
    });
    return flag;
  };
  // 服务器(记录中发送，修改记录，单条)
  const sendMessageByServerHistory = async (accNbr: string, orderContent: string, requestTime: string, id: number) => {
    // 获取发送人信息
    userId.value = userStore.userId;
    account.value = userStore.account;
    const item = {
      accNbr,
      orderContent,
      requestTime,
      sender: `${account.value}(${userId.value})`,
    };
    let state = '';
    await sendUccpMessage(item).then(async () => {
      message.success('发送成功！');
      state = '发送成功';
    }).catch((error) => {
      state = `发送失败:${error}`;
      message.success('发送失败！');
    });
    const messagedata = getSendData(id, accNbr, orderContent, requestTime, state);
    await updateUccpMessageParams(messagedata);
  };
  return {
    sendMessage,
    sendMessageHistory,
    sendMessageByServer,
    sendMessageByServerHistory,
    getCurrentTimeFormatted,
  };
}
